//
//  TW_ViewController.m
//  Tower
//
//  Created by Jonear on 14-5-10.
//  Copyright (c) 2014年 Jonear. All rights reserved.
//

#import "TW_ViewController.h"
#import "TW_MyScene.h"

#define ControlButtonTag 120
@implementation TW_ViewController
{
    TW_MyScene *_mainScene;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    
    // Create and configure the scene.
    _mainScene = [TW_MyScene sceneWithSize:skView.bounds.size];
    _mainScene.scaleMode = SKSceneScaleModeAspectFill;
    
    // Present the scene.
    [skView presentScene:_mainScene];
    
    //
    [self initControllerButton];
    
//    [self drawLines];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (void)initControllerButton
{
    UIButton *upButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 160, 50, 50)];
    [upButton setBackgroundColor:[UIColor redColor]];
    [upButton setTitle:@"上" forState:UIControlStateNormal];
    [upButton setTag:ControlButtonTag];
    [upButton.layer setMasksToBounds:YES];
    [upButton.layer setCornerRadius:25];
    [upButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDown];
//    [upButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
    [self.view addSubview:upButton];
    
    UIButton *downButton = [[UIButton alloc] initWithFrame:CGRectMake(70, 240, 50, 50)];
    [downButton setBackgroundColor:[UIColor grayColor]];
    [downButton setTitle:@"下" forState:UIControlStateNormal];
    [downButton setTag:ControlButtonTag+1];
    [downButton.layer setMasksToBounds:YES];
    [downButton.layer setCornerRadius:25];
    [downButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDown];
//    [downButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
    [self.view addSubview:downButton];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 200, 50, 50)];
    [leftButton setBackgroundColor:[UIColor greenColor]];
    [leftButton setTitle:@"左" forState:UIControlStateNormal];
    [leftButton setTag:ControlButtonTag+2];
    [leftButton.layer setMasksToBounds:YES];
    [leftButton.layer setCornerRadius:25];
    [leftButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDown];
//    [leftButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
    [self.view addSubview:leftButton];
    
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(110, 200, 50, 50)];
    [rightButton setBackgroundColor:[UIColor blueColor]];
    [rightButton setTitle:@"右" forState:UIControlStateNormal];
    [rightButton setTag:ControlButtonTag+3];
    [rightButton.layer setMasksToBounds:YES];
    [rightButton.layer setCornerRadius:25];
    [rightButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDown];
//    [rightButton addTarget:self action:@selector(moveButtonClick:) forControlEvents:UIControlEventTouchDownRepeat];
    [self.view addSubview:rightButton];
}

- (void)drawLines
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(150, 0, 1, 320)];
    [lineView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:lineView];
    
    for (int i=0; i<11; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(150, i*32-0.5, 568-130, 1)];
        [line setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:line];
    }
    
    for (int i=0; i<15; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(150+i*32, 0, 1, 320)];
        [line setBackgroundColor:[UIColor redColor]];
        [self.view addSubview:line];
    }
}

- (void)moveButtonClick:(UIButton *)sender
{
    int index = sender.tag-ControlButtonTag;
    [_mainScene heroMoveTo:index];
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    } else {
        return UIInterfaceOrientationMaskAll;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

@end
